home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / AntiAlias / antialias.p < prev    next >
Text File  |  1995-06-15  |  3KB  |  120 lines

  1. {Based on a demo by oster@netcom.com (David Phillip Oster),}
  2. {posted on alt.sources.mac in 1994.}
  3.  
  4. {*******************************}
  5. {This version by Ingemar Ragnemalm.}
  6. {Uses an offscreen with selectable depth. With depth 1, as in the original program,}
  7. {I didn't manage to get any anti-aliasing at all. However, with depth 2, it changes}
  8. {altogether. Modify the kOffDepth constant below to try it.}
  9. {*******************************}
  10.  
  11. unit AntiAlias;
  12.  
  13. interface
  14.     uses
  15. {$IFC UNDEFINED THINK_PASCAL}
  16.         Types, QuickDraw, ToolUtils, Events, Menus, Dialogs, Fonts, Resources, Devices,
  17. {$ENDC}
  18.         QDOffscreen;
  19.  
  20.     function ADrawString (text: Str255): OSErr;
  21.     function ADrawText (text: Ptr; off, len: integer): OSErr;
  22.  
  23. implementation
  24.  
  25.     const
  26. {Offscreen Depth. Originally 1. Set to 2 or more for a much nicer result .}
  27.         kOffDepth = 2;
  28.  
  29.  
  30. (* ADrawString - draw a string using copybits antialiasing *)
  31.     function ADrawString (text: Str255): OSErr;
  32.     begin
  33.         ADrawString := ADrawText(@text, 1, ord(text[0]));
  34.     end;
  35.  
  36. (* ADrawText - draw a text using copybits antialiasing *)
  37.     function ADrawText (text: Ptr; off, len: integer): OSErr;
  38.         var
  39.             errCode: OSErr;
  40.             savePort: CGrafPtr;
  41.             saveDev: GDHandle;
  42.             bigGWorld: GWorldPtr;
  43.             curPort: GrafPtr;
  44.             fInfo: FontInfo;
  45.             theTextFace: Style;
  46.             bwRect, destRect: Rect;
  47.             theTextSize, theTextFont, width, height, smallAscent: Integer;
  48.     begin
  49.         GetPort(curPort);
  50.         GetFontInfo(fInfo);
  51.  
  52.         smallAscent := fInfo.ascent;
  53.         theTextFace := curPort^.txFace;
  54.         theTextFont := curPort^.txFont;
  55.         theTextSize := curPort^.txSize;
  56.         TextSize(theTextSize * 4);
  57.  
  58.         width := TextWidth(text, off, len);
  59.         width := width + 32 - width mod 32;
  60.  
  61.         GetFontInfo(fInfo);
  62.         height := fInfo.ascent + fInfo.descent;
  63.         height := height + 4 - height mod 4;
  64.  
  65.         TextSize(theTextSize);
  66.  
  67.         bwRect.left := 0;
  68.         bwRect.top := 0;
  69.         bwRect.right := width;
  70.         bwRect.bottom := height;
  71.  
  72.         destRect := bwRect;
  73.         destRect.right := destRect.right div 4;
  74.         destRect.bottom := destRect.bottom div 4;
  75.  
  76.         OffsetRect(destRect, curPort^.pnLoc.h, curPort^.pnLoc.v - smallAscent);
  77.  
  78. {If I use a bigger depth than 1 for the GWorld below, the result is much nicer! But slower!}
  79. {$IFC UNDEFINED THINK_PASCAL}
  80.         errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, 0);
  81.         if noErr <> errCode then
  82.             errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, useTempMem);
  83. {$ELSEC}
  84.         errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, []);
  85.         if noErr <> errCode then
  86.             errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, [useTempMem]);
  87. {$ENDC}
  88.         if noErr <> errCode then
  89.             begin
  90.                 ADrawText := errCode;
  91.                 exit(ADrawText);
  92.             end;
  93.  
  94.         if LockPixels(GetGWorldPixMap(bigGWorld)) then
  95.             ;
  96.  
  97.         GetGWorld(savePort, saveDev);
  98.         SetGWorld(bigGWorld, nil);
  99.  
  100.         EraseRect(bwRect);
  101.  
  102.         TextSize(theTextSize * 4);
  103.         TextFont(theTextFont);
  104.         TextFace(theTextFace);
  105.  
  106.         MoveTo(0, fInfo.ascent);
  107.         DrawText(text, off, len);
  108.  
  109.         SetGWorld(savePort, saveDev);
  110.  
  111.         CopyBits(GrafPtr(bigGWorld)^.portBits, curPort^.portBits, bwRect, destRect, BitOr(ditherCopy, curPort^.txMode), nil);
  112.         Move(destRect.right - destRect.left, 0);
  113.  
  114.         UnlockPixels(GetGWorldPixMap(bigGWorld));
  115.         DisposeGWorld(bigGWorld);
  116.  
  117.         ADrawText := errCode;
  118.     end;
  119.  
  120. end.